package de.tewdreyer.android.erc;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LogMessage extends Activity {
	/**
	 * implementation of an android View.OnClickListener
	 * @see android.view.View.OnClickListener
	 */
	private class SendOnClickListener implements OnClickListener {
        /**
         * perform a http post and send a multipart entity to an url
         * @param view view that has been clicked
         * @see android.view.View.OnClickListener#onClick(View)
         */
		@Override
		public void onClick(View view) {
            /* test if deviceIdentifier is set */
            if(ERC.deviceIdentifier == null){
                Toast.makeText(getApplicationContext(),"no device selected ..",Toast.LENGTH_SHORT).show();
                return;
            }

            /* read summary text or set if is empty */
            String summary = edtTxtSummary.getText().toString();
            if(summary.length() == 0){
                summary = "posted by android phone";
            }

            /* break if message is an empty string */
            String message = edtTxtMessage.getText().toString();
            if(message.length() == 0){
                /* notify user */
                Toast.makeText(getApplicationContext(),"no message to send ..",Toast.LENGTH_SHORT).show();
                return;
            }
            
            /* notify user about action */
            Toast.makeText(getApplicationContext(), "trying to post ..", Toast.LENGTH_SHORT).show();
            
            /* create multipart entity and add elements */
            MultipartEntity me = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            me.addPart("comment_summary", HttpPoster.createStringBody(summary));
            me.addPart("comment_text", HttpPoster.createStringBody(message));
            me.addPart("comment_action", HttpPoster.createStringBody("save"));
            me.addPart("comment_type", HttpPoster.createStringBody("log"));
            me.addPart("comment_index", HttpPoster.createStringBody("0"));

            HttpPoster poster = new HttpPoster(ERC.logMessageUrl + ERC.deviceIdentifier, me);

            /* perform POST */
            int returnCode = poster.performPost();
            if(returnCode != poster.ErrorCode()){
                /* http status code 2xx means success and 3xx means redirection
            	   both are ok */
            	if(returnCode >= 200 && returnCode < 400){
                	/* notify user about success */
                    Toast.makeText(getApplicationContext(),"http POST successful",Toast.LENGTH_SHORT).show();

                    /* reset textboxes */
                    edtTxtMessage.setText("");
                    edtTxtSummary.setText("");
                } else {
                	/* notify user about failure */
                    Toast.makeText(getApplicationContext(),"http POST failed",Toast.LENGTH_SHORT).show();
                }
            } else {
            	/* notify user about failure */
                Toast.makeText(getApplicationContext(),"http POST failed",Toast.LENGTH_SHORT).show();
            }
        }
	}
	
	/**
	 * references to visual objects
	 */
	private EditText edtTxtSummary;
	private EditText edtTxtMessage;
	private Button   btnSend;
	
	/**
     * initialize all fields and set listener 
     * @param inState activity's previous state 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
	public void onCreate(Bundle inState) {
        super.onCreate(inState);

        /* display layout */
        setContentView(R.layout.log_message);
        
        /* initialize fields */
        edtTxtSummary = (EditText)findViewById(R.id.edtTxtSummary);
        edtTxtMessage = (EditText)findViewById(R.id.edtTxtMessage);
        btnSend       = (Button)findViewById(R.id.btnSend);
        
        /* set listener */
        btnSend.setOnClickListener(new SendOnClickListener());
    }
}